package com.gxa.cart.service.impl;

import com.gxa.cart.pojo.CartItem;
import com.gxa.cart.service.CartService;
import com.gxa.common.exception.BusinessException;
import com.gxa.common.exception.MessageCode;
import com.gxa.goods.pojo.Sku;
import com.gxa.goods.service.SkuService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.rpc.RpcContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.ObjectUtils;

import java.util.Map;

/**
 * Created by bill on 2021/1/6
 **/
@org.apache.dubbo.config.annotation.Service
@Slf4j
public class CartServiceImpl implements CartService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private SkuService skuService;


    public static final String CART="cart:";

    @Override
    public void addGoodsToCart(Long userId,String  skuId,Integer num) throws BusinessException {

        Object valueId = RpcContext.getContext().getAttachment("userId");
        log.info("dubbo隐式传参获取参数:{}",valueId);

        Sku sku = skuService.findById(skuId);

        CartItem cartItem = (CartItem) redisTemplate.boundHashOps(CART + userId).get(skuId);

        if(!ObjectUtils.isEmpty(cartItem)){
            //1.先判断该商品是否添加到购物车中，如果已经添加，增加数量
            if(cartItem.getNum()+num>sku.getNum()){
                //初步库存不足
                throw new BusinessException(MessageCode.ERROR);
            }
            cartItem.setNum(cartItem.getNum()+num);
            cartItem.setTotalPrice(cartItem.getPrice()*cartItem.getNum());
            //2.放入更新的结果到redis中
            redisTemplate.boundHashOps(CART + userId).put(skuId,cartItem);
            return;
        }

        //2.如果没有添加，就创建一个新的购物项
        if(num>sku.getNum()){
            //初步库存不足
            throw new BusinessException(MessageCode.ERROR);
        }

        cartItem=new CartItem();
        cartItem.setNum(num);
        cartItem.setSkuId(skuId);
        cartItem.setImage(sku.getImage());
        cartItem.setName(sku.getName());
        cartItem.setPrice(sku.getPrice());

        //设置小计  单价*数量
        cartItem.setTotalPrice(sku.getPrice()*num);

        //3.放入redis中
        redisTemplate.boundHashOps(CART + userId).put(skuId,cartItem);

    }

    @Override
    public void updateCartItemNum(Long userId,String  skuId, Integer num) {

        CartItem cartItem = (CartItem) redisTemplate.boundHashOps(CART + userId).get(skuId);

        if(!ObjectUtils.isEmpty(cartItem)){
            //1.先判断该商品是否添加到购物车中，如果已经添加，增加数量
            cartItem.setNum(num);
            cartItem.setTotalPrice(cartItem.getPrice()*cartItem.getNum());
            //2.放入更新的结果到redis中
            redisTemplate.boundHashOps(CART + userId).put(skuId,cartItem);
        }

    }

    @Override
    public void removeCartItem(Long userId,String  skuId) {
        redisTemplate.boundHashOps(CART + userId).delete(skuId);
    }

    @Override
    public void clearCart(Long userId) {
        redisTemplate.delete(CART+ userId);
    }

    @Override
    public CartItem queryUserCartItem(Long userId,String  skuId) throws BusinessException {
        CartItem cartItem = (CartItem) redisTemplate.boundHashOps(CART + userId).get(skuId);
        if(ObjectUtils.isEmpty(cartItem)){
            throw new BusinessException(MessageCode.ERROR);
        }
         return cartItem;
    }

    @Override
    public Map queryUserCart(Long userId) {
        return redisTemplate.boundHashOps(CART + userId).entries();
    }
}
